package com.ccl.iot.asynctask;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.bean.IrDataBean;
import com.ccl.iot.device.IOTSubBLir;
import com.ccl.iot.ir.DBUtils;
import com.ccl.iot.publics.Messager;
import com.google.gson.Gson;
import com.mysql.jdbc.Connection;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.SQLException;

//保存红外
public class SaveIrTask extends CustomTask<String, Void, Long> {
    private IOTSubBLir mSubBLir;

    public SaveIrTask(Context context, IOTSubBLir subBLir) {
        super(context, "保存中...");
        mSubBLir = subBLir;
    }

    @Override
    protected Long doInBackground(String... params) {
        long fixed = 0;
        Connection dbConn = DBUtils.getDBConn();
        if (dbConn != null) {
            int subId;
            if (mSubBLir.isTmp()) {
                subId = (int) DBUtils.getSubDevVer(mSubBLir.getDbId(), dbConn);
//                mSubBLir.setName(subId, mSubBLir.getName());
                mSubBLir.setName(mSubBLir.getName());
            } else {
                subId = (int) mSubBLir.getSubId();
            }

            //    aDevID		: bigint unsigned
            //    aSubDevID	: int unsigned
            //    aDevType	: varchar(32)
            //    aKeyType	: varchar(32)
            //    aCodeType	: varchar(32)
            //    aCode		: varbinary(255)
            Log.e("SubId", "SubId: " + subId);
            String keyType = params[0];
            String keyName = params[1];
            if (DBUtils.CODE_TYPE_API.equals(keyType)) {
                CodeviewBean api = mSubBLir.getIRCode().get(DBUtils.CODE_TYPE_API);
                if (api != null && api.ac != null) {
                    Gson gson = new Gson();
                    String code = gson.toJson(new IrDataBean(api.ac));
                    if (code != null) {
                        fixed = DBUtils.addIrCode(mSubBLir.getDbId(), subId, mSubBLir.getDevType(), keyType, keyName, DBUtils.CODE_TYPE_API, code, dbConn);
                        try {
                            FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath()+"/GSON.txt"));
                            fos.write(code.getBytes());
                            fos.flush();
                            fos.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    if (fixed != 0) {
                        CodeviewBean bean = new CodeviewBean(mSubBLir.getDbId(), subId, mSubBLir.getDevType(), api.ac, DBUtils.CODE_TYPE_API, fixed);
                        mSubBLir.getSuperDev().addIrCode(bean);
                    }
                }
            } else {
                if (!mSubBLir.isTmp() && mSubBLir.getIRCode().containsKey(keyType)) {
                    String result = DBUtils.updateCode(mSubBLir.getDbId(), subId, keyType, keyName, DBUtils.CODE_TYPE_FIXED, params[2], dbConn);
                    if ("OK".equals(result)) {
                        fixed = mSubBLir.getIRCode().get(keyType).CodeID;
                    } else {
                        fixed = 0;
                    }
                } else {
                    fixed = DBUtils.addIrCode(mSubBLir.getDbId(), subId, mSubBLir.getDevType(), keyType, keyName, DBUtils.CODE_TYPE_FIXED, params[2], dbConn);
                }

                if (fixed != 0) {
                    CodeviewBean bean = new CodeviewBean(mSubBLir.getDbId(), subId, mSubBLir.getDevType(), keyType, keyName, DBUtils.CODE_TYPE_FIXED, params[2], fixed);
                    mSubBLir.getSuperDev().addIrCode(bean);
                }
            }

            try {
                dbConn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return fixed;
    }

    @Override
    protected void onPostExecute(Long result) {
        super.onPostExecute(result);
        if (result != 0) {
            Messager.Show("保存成功");
        } else {
            Messager.Show("保存失败");
        }
    }
}